/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.isd.global.util;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;
import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRXlsAbstractExporterParameter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import net.sf.jasperreports.engine.util.JRLoader;


/**
 *
 * @author marceloagustini
 */
public class ReportFactory {
    
    Context ctx;
    DataSource ds;
    Connection conexion;

    public ReportFactory() throws NamingException, SQLException {
        ctx = new InitialContext();
        ds = (DataSource) ctx.lookup("jdbc/SQLServer");
        conexion = ds.getConnection();
        conexion.setAutoCommit(true);
    }

    public void generarReportePDF(String reportName,String nombreSalida,Map parameters) throws NamingException, SQLException, JRException, IOException {
               
        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(getClass().getClassLoader().getResourceAsStream(reportName));        
        JasperPrint print = JasperFillManager.fillReport(jasperReport, parameters, conexion);

        byte[] bytes = JasperExportManager.exportReportToPdf(print);
        
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();

        response.addHeader("Content-disposition","inline;filename="+nombreSalida+".pdf");
        //response.addHeader("Content-disposition","attachment;filename="+nombreSalida+".pdf");
        response.setContentLength(bytes.length);
        response.getOutputStream().write(bytes);
        response.setContentType("application/pdf");
        context.responseComplete();
    }

   public void generarReporteExcel(String reportName,String nombreSalida,Map parameters ) throws JRException{

        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(getClass().getClassLoader().getResourceAsStream(reportName));        
        JasperPrint print = JasperFillManager.fillReport(jasperReport, parameters, conexion);

        JRXlsExporter exportador = new JRXlsExporter();
        exportador.setParameter(JRExporterParameter.JASPER_PRINT, print);
        exportador.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "C:\\excel.xls");
        exportador.setParameter(JRExporterParameter.IGNORE_PAGE_MARGINS, true);
        exportador.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND, false);
        exportador.setParameter(JRXlsAbstractExporterParameter.IS_IGNORE_CELL_BORDER, false);
        exportador.setParameter(JRXlsAbstractExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS,true);
        exportador.setParameter(JRXlsAbstractExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,true);
        exportador.setParameter(JRXlsAbstractExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,true);
        exportador.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE,true);

//        FileInputStream entrada = new FileInputStream("C:especificamos la ruta en donde esta el archivo de excel en blanco que mostrara lo que mi reporte hecho en ireport genera\\PruebaExcel.xls");
//byte[] lectura = new byte[entrada.available()];
//entrada.read(lectura);
//response.setContentType("application/vnd.ms-excel");
//response.setHeader("Content-Disposition","attachment; filename=PruebaExcel.xls");
//response.setContentLength(lectura.length);
//response.getOutputStream().write(lectura);
//response.getOutputStream().flush();
//response.getOutputStream().close();
//entrada.close();

    try {
        exportador.exportReport();
    } catch (JRException e) {
        e.printStackTrace();
    }

  }


}
